package com.library.books.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.library.books.domain.Books;
import com.library.books.domain.BooksCopy;
import com.library.books.domain.vo.BooksJoinBooksCopyJoinBooksRule;
import com.library.books.domain.vo.BooksJoinBooksCopyJoinBooksRuleJoinBookShelf;
import com.library.books.mapper.BooksMapper;
import com.library.books.service.IBooksCopyService;
import com.library.books.service.IBooksService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 书籍Service业务层处理
 *
 * @author hb
 * @date 2024-10-14
 */
@Service
public class BooksServiceImpl extends ServiceImpl<BooksMapper, Books> implements IBooksService
{

    @Resource
    private IBooksService bookService;

    @Resource
    private IBooksCopyService booksCopyService;


    /**
     * 查询书籍列表
     *
     *
     * @param books 书籍
     * @return 书籍
     */
    @Override
    public List<Books> selectBooksList(Books books) {
        return baseMapper.selectBooksList(books);
    }

    /**
     * 根据RFID查询出书籍信息和规则信息
     *
     * @param rfid rfid编号
     * @return 书籍拼接书籍副本和书籍规则实体类
     */
    @Override
    public List<BooksJoinBooksCopyJoinBooksRule> selectBooksInfoJoinBooksRuleInfoListByRfid(List<String> rfid) {
        return baseMapper.selectBooksInfoJoinBooksRuleInfoListByRfid(rfid);
    }

    /**
     * 根据索书号查询出书籍信息和规则信息
     *  - 小分类索书号
     *
     * @param license 索书号
     * @return 书籍拼接书籍副本和书籍规则实体类
     */
    @Override
    public BooksJoinBooksCopyJoinBooksRule selectBooksInfoJoinBooksRuleInfoListByLicense(String license) {
        return baseMapper.selectBooksInfoJoinBooksRuleInfoListByLicense(license);
    }

    /**
     * 软删除
     *
     * @param ids id
     * @return 结果
     */
    @Override
    public boolean softDeletion(Long[] ids) {
        QueryWrapper<BooksCopy> booksCopyQueryWrapper = new QueryWrapper<>();
        booksCopyQueryWrapper.in("books_id", Arrays.asList(ids)).eq("books_copy_soft", 0L);
        List<BooksCopy> list = booksCopyService.list(booksCopyQueryWrapper);

        if (!list.isEmpty()) {
            throw new RuntimeException("该书籍存在副本！");
        }

        ArrayList<Books> booksList = new ArrayList<>();
        for (Long booksId : ids) {
            Books books = new Books();
            books.setBooksId(booksId);
            books.setBooksSoft(1L);
            booksList.add(books);
        }
        return bookService.updateBatchById(booksList);
    }

    /**
     * 查询书籍回收列表
     *
     * @param books 书籍
     * @return 书籍回收集合
     */
    @Override
    public List<BooksJoinBooksCopyJoinBooksRule> selectBooksRecycleList(Books books) {
        return baseMapper.selectBooksRecycleList(books);
    }

    /**
     * 恢复书籍
     *
     * @param ids id
     * @return 结果
     */
    @Override
    public boolean recoveryBooks(Long[] ids) {
        QueryWrapper<Books> booksQueryWrapper = new QueryWrapper<>();
        booksQueryWrapper.in("books_id", Arrays.asList(ids));
        List<Books> booksList = new ArrayList<>();
        for (Long id : ids) {
            Books books = new Books();
            books.setBooksId(id);
            books.setBooksSoft(0L);
            booksList.add(books);
        }
        return bookService.updateBatchById(booksList);
    }

    /**
     * 一键恢复书籍和书籍副本
     *
     * @param booksId id
     * @return 结果
     */
    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public boolean oneKeyRecoveryBooksAndBooksCopy(Long[] booksId) {
        QueryWrapper<Books> booksQueryWrapper = new QueryWrapper<>();
        booksQueryWrapper.in("books_id", Arrays.asList(booksId));

        QueryWrapper<BooksCopy> booksCopyQueryWrapper = new QueryWrapper<>();
        booksCopyQueryWrapper.in("books_id", Arrays.asList(booksId));

        Books books = new Books();
        books.setBooksSoft(0L);

        BooksCopy booksCopy = new BooksCopy();
        booksCopy.setBooksCopySoft(0L);

        bookService.update(books,booksQueryWrapper);
        return booksCopyService.update(booksCopy,booksCopyQueryWrapper);
    }

    /**
     * 查询书籍信息
     *  - 书籍信息（必须返回）
     *  - 书籍副本（）
     *  - 书籍规则（）
     *  - 书籍存放书架（）
     * @param join 查询条件
     * @return list
     */
    @Override
    public BooksJoinBooksCopyJoinBooksRuleJoinBookShelf selectBooksInfoJoinBooksRuleJoinBookShelfInfo(BooksJoinBooksCopyJoinBooksRuleJoinBookShelf join) {
        return this.getBaseMapper().selectBooksInfoJoinBooksRuleJoinBookShelfInfo(join);
    }

}
